<template>
	<el-dialog v-model="dialogFormVisible" title="删除操作" width="30%" center>
		<span>是否去掉此用户的管理员职位？删除后此用户将重新展现在用户列表中</span>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" @click="deleteadmin">
					确定
				</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
	import { onBeforeMount, onBeforeUnmount, ref } from 'vue'
	import { bus } from "@/utils/mitt.js"
	import {
		changeIdentityToUser
	} from '@/api/userinfo.js'
	import { ElMessage } from 'element-plus';

	const userid = ref()
	bus.on('deleteId', async (id : number) => {
		userid.value = id
	})

	const emit = defineEmits(['success'])

	const deleteadmin = async () => {
		const res = await changeIdentityToUser(userid.value)
		if (res.status == 0) {
			ElMessage({
				message: '对管理员降级成功',
				type: 'success',
			})
			emit('success')
			dialogFormVisible.value = false
		} else {
			ElMessage.error('对管理员降级失败')
		}
	}

	// 控制弹窗开关
	const dialogFormVisible = ref(false)

	// 打开创建管理员的弹窗
	const open = () => {
		dialogFormVisible.value = true
	}

	defineExpose({
		open
	})

	onBeforeUnmount(() => {
		bus.all.clear()
	})
</script>

<style lang="scss" scoped>
</style>